package com.nttuan.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.nttuan.model.dao.service.TaskService;
import com.nttuan.model.entity.Alarm;
import com.nttuan.model.entity.Task;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by TuanNT on 11/26/2014.
 */
public class LogSupport {

    public static final String LOG_ONLINE="online";
    public static final String LOG_SETTING="setting";
    public static final String LOG_ALARM="alarm";
    public static final String KEY_LAST_UPDATE="update";
    public static final String KEY_ALARMS="alarms";
    public static final String KEY_LAST_TIME = "lasttime";
    public static final String KEY_HELP = "help";


    public static List<Alarm> getLogAlarm(Context context, Date now, TaskService service){
        List<Alarm> result = new ArrayList<Alarm>();
        SharedPreferences pf = context.getSharedPreferences(LOG_ALARM, Context.MODE_PRIVATE);
        String values = pf.getString(KEY_ALARMS,null);
        Log.d("alarmService", "get log 1/" + values);

        if(values != null && !values.equals("")){
            String[] arrAlarm = values.split(",");
            String[] val;
            Alarm alarmItem;
            Log.d("alarmService", "get log");
            for (String alarm:arrAlarm){
               val = alarm.split(";");
               Date alarmAt = Support.toDateTime(val[0],false);
               Task task = service.task(Integer.parseInt(val[1]));
               if(task == null)
                   continue;
               if(alarmAt.getTime() - now.getTime() == 0) {
                   Log.d("alarmService", "alarm");
                   alarmItem = new Alarm();
                   alarmItem.setAlarmAt(alarmAt);
                   alarmItem.setStated(false);
                   alarmItem.setTask(task);
                   result.add(alarmItem);
                   continue;
               }
                if(stared(now, task)){
                    Log.d("alarmService", "started ok");
                    alarmItem = new Alarm();
                    alarmItem.setAlarmAt(alarmAt);
                    alarmItem.setStated(true);
                    alarmItem.setTask(task);
                    result.add(alarmItem);
                    continue;
                }
            }
        }
        return result;
    }

    private static boolean stared(Date now, Task task){
        if(task.getRemind() > 1440)
            return false;
        Date start =Support.removeMilliSecond(Support.sysDate(now, task.getStart()));
        if(start.getTime() - now.getTime() == 0)
            return true;
        return false;
    }
    public static boolean lastUpdate(Context context){
        Date currentDate = Calendar.getInstance().getTime();
        SharedPreferences pf = context.getSharedPreferences(LOG_ALARM, Context.MODE_PRIVATE);
        String slastUpdate = pf.getString(KEY_LAST_UPDATE,null);
        if(slastUpdate != null)
            return Support.equalDate(currentDate,Support.toDateTime(slastUpdate, false));
        return false;
    }
    public static void saveLog(Context context, List<Alarm> alarms){
        Log.d("logSupport","save log");
        Date dateUpdate = Calendar.getInstance().getTime();
        StringBuilder values = new StringBuilder();
        values.append("");
        for(Alarm item: alarms) {
            values.append(Support.toString(item.getAlarmAt()) +";"+ item.getTask().getTaskId());
            values.append(",");
        }
        Log.d("logSupport","save/" + values.toString());
        SharedPreferences pf = context.getSharedPreferences(LOG_ALARM, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pf.edit();
        editor.putString(KEY_LAST_UPDATE,Support.toString(dateUpdate));
        editor.putString(KEY_ALARMS, values.toString());
        editor.commit();
    }
    public static void saveLastTimeNotifi(Context context, Date lastUpdate){
        SharedPreferences pf = context.getSharedPreferences(LOG_ALARM, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pf.edit();
        editor.putString(KEY_LAST_TIME, Support.toString(lastUpdate));
        editor.commit();
    }
    public static Date lastTimeNotifi(Context context){
        Date now = Calendar.getInstance().getTime();
        SharedPreferences pf = context.getSharedPreferences(LOG_ALARM, Context.MODE_PRIVATE);
        String slastUpdate = pf.getString(KEY_LAST_TIME,null);
        Date date = null;
        if(slastUpdate == null) {
            saveLastTimeNotifi(context, now);
            return now;
        }
        else
            return Support.toLongDateTime(slastUpdate);
    }
    public static void saveUpdateServer(Context context, Date lastUpdate){
        SharedPreferences pf = context.getSharedPreferences(LOG_ONLINE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pf.edit();
        editor.putString(KEY_LAST_TIME, Support.toStringDateTimeMili(lastUpdate));
        editor.commit();
    }
    public static String slastUpdateServer(Context context){

        SharedPreferences pf = context.getSharedPreferences(LOG_ONLINE, Context.MODE_PRIVATE);
        String slastUpdate = pf.getString(KEY_LAST_TIME,null);
        if(slastUpdate == null) {
            // return Support.toString(now);
            String now = JSONParser.currentTimeServer();
            SharedPreferences.Editor editor= pf.edit();
            editor.putString(KEY_LAST_TIME, now);
            editor.commit();
            return now;
        }
        else
            return slastUpdate;
    }
    public static String getKeyHelp(Context context){
        SharedPreferences pf = context.getSharedPreferences(LOG_SETTING, Context.MODE_PRIVATE);
        return pf.getString(KEY_HELP,"SOS");
    }
    public static void setKeyHelp(Context context, String keyHelp){
        SharedPreferences pf = context.getSharedPreferences(LOG_SETTING, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pf.edit();
        if(keyHelp != null && !keyHelp.equals(""))
            editor.putString(KEY_HELP, keyHelp);
        else
            editor.putString(KEY_HELP, "SOS");
        editor.commit();
    }
}
